import React from 'react';
import { Button } from 'antd';
import { WeaTools, WeaLocaleProvider, WeaDialog, WeaSearchGroup, WeaFormItem, WeaTab } from 'ecCom';
import { toJS } from 'mobx';
import { WeaForm, WeaTableNew, WeaSwitch } from 'comsMobx';

const { TableStore } = WeaTableNew;
const WeaTable = WeaTableNew.WeaTable;
const getLabel = WeaLocaleProvider.getLabel;
let tableStore = new TableStore();
const searchForm = new WeaForm();
// import './style/';
import WeaLicenseSubmitCluster from '../wea-license-submit/';
import WeaNonStandard from '../wea-non-standard/';

class WeaLicenseCluster extends React.Component {
  state = { loading: false, nsusecnt: 0, data: {}, showSearchAd: false };


  constructor(props) {
    super(props);
    this.getButtons = this.getButtons.bind(this);
    this.onCancel = this.onCancel.bind(this);
    // this.onShow = this.onShow.bind(this);
    this.searchForm = new WeaForm();
    this.getCondition();
    this.searchForm.setCondition(this.condition);
    this.getTable(false);
  }
  componentWillReceiveProps(nextProps) {
    const { visible } = nextProps;
    if (visible) {
      this.getTable(false);
    }
  }
  render() {
    const { visible } = this.props;
    const { loading, showSearchAd } = this.state;

    return (
      <React.Fragment ecId={`${this && this.props && this.props.ecId || ''}_React.Fragment@8rawx8`}>
        <WeaNonStandard ecId={`${this && this.props && this.props.ecId || ''}_WeaNonStandard@kigbtu`} ref="WeaNonStandard" />
        <WeaLicenseSubmitCluster ecId={`${this && this.props && this.props.ecId || ''}_WeaLicenseSubmitCluster@i4n24d`} ref="WeaLicenseSubmitCluster" />
        <WeaDialog ecId={`${this && this.props && this.props.ecId || ''}_WeaDialog@h3d2kz`}
          visible={visible}
          loading={loading}
          title={getLabel(18014, '授权信息')}
          icon="icon-portal-license"
          iconBgcolor="#a7adb5"
          style={{ width: 1000, height: 563 }}
          zIndex={100}
          hasScroll={false}
          buttons={this.getButtons()}
          onCancel={this.onCancel}
        >

          <WeaTab ecId={`${this && this.props && this.props.ecId || ''}_WeaTab@9pmalo`}
            datas={[]}
            keyParam="viewcondition" // 主键
            countParam="groupid" // 数量
            searchType="{['base','advanced']}"
            searchsBaseValue={searchForm.getFormParams().ip}
            advanceHeight={160}
            searchsAd={this.getAdvancedSearch(searchForm, toJS(this.state.advancedSearchConditionInfo), false, 2)}
            showSearchAd={showSearchAd}
            buttonsAd={this.getTabButtonsAd()}
            setShowSearchAd={bool =>
              this.setPanelStatus(bool)
            }
            hideSearchAd={() =>
              this.setPanelStatus(false)
            }
            onSearch={() => this.getTable(true)}
            onSearchChange={(value) => {
              searchForm.updateFields({ ip: value });
            }}
          />
          <WeaTable ecId={`${this && this.props && this.props.ecId || ''}_WeaTable@vxugqs`}
            comsWeaTableStore={tableStore}
            hasOrder={true}
            needScroll={true}
            uniqueId={true}
            onOperatesClick={this.onOperatesClick}
            getColumns={this.getColumns}
          />
        </WeaDialog>
      </React.Fragment>
    );
  }

  getButtons() {
    const { data } = this.state;
    let buttons = [];
    if (data.canEdit) {
      buttons.push(<Button ecId={`${this && this.props && this.props.ecId || ''}_Button@t875w5`} type="primary" onClick={() => this.refs.WeaLicenseSubmit.onShow()}>{getLabel(18640, '提交license')}</Button>);
    }
    return buttons;
  }

  // onCancel() {
  //   this.setState({ visible: false });
  // }

  // onShow() {
  //   this.setState({ visible: true, loading: true });
  //   WeaTools.callApi('/api/system/license/licenseInfo', 'POST', {}).then(data => {
  //     this.setState({ loading: false, data: data });
  //   });

  //   WeaTools.callApi('/api/portal/unStandardFuncInfo/count', 'POST', {}).then(data => {
  //     this.setState({ nsusecnt: data.onCount });
  //   });
  // }
  onCancel = () => {
    this.props.onHide();
    searchForm.resetForm();
  };

  getTabButtonsAd() {
    const btn = [
      (<Button ecId={`${this && this.props && this.props.ecId || ''}_Button@l5rkn4`} type="primary"
        onClick={() => {
          this.getTable(true);
          this.setPanelStatus(false);
        }}
      >{getLabel(197, '搜索')}</Button>),
      (<Button ecId={`${this && this.props && this.props.ecId || ''}_Button@mx57u1`} onClick={() => { searchForm.resetForm(); }}>{getLabel(2022, '重置')}</Button>),
      (<Button ecId={`${this && this.props && this.props.ecId || ''}_Button@5jysiu`} onClick={() => this.setPanelStatus(false)}>{getLabel(31129, '取消')}</Button>),
    ];
    return btn;
  }

  getCondition() {
    WeaTools.callApi('/api/system/license/searchCondition', 'GET', {}).then((res) => {
      this.setState({ advancedSearchConditionInfo: res.conditions });
      searchForm.initFormFields(res.conditions);
    });
  }

  setPanelStatus(bool) {
    this.setState({ showSearchAd: bool });
  }

  getTable(params) {
    WeaTools.callApi('/api/system/license/listLicense', 'POST', searchForm.getFormParams()).then((res) => {
      // this.setState({
      //   noright: res.noright
      // });
      if (res.sessionkey) {
        if (params) {
          tableStore.getDatas(res.sessionkey, 1);
        } else {
          tableStore.getDatas(res.sessionkey);
        }
      }
    });
  }

  getAdvancedSearch = (form, datas, needTigger = true, col = 1, labelCol, fieldcol) => {
    const { isFormInit } = form;
    let group = [];
    const formParams = form.getFormParams();
    isFormInit && datas && datas.map((c) => {
      let items = [];
      c.items.map((fields) => {
        items.push({
          com: (<WeaFormItem ecId={`${this && this.props && this.props.ecId || ''}_WeaFormItem@zkowx6`}
            label={`${fields.label}`}
            labelCol={{ span: labelCol || `${fields.labelcol}` }}
            viewAttr={fields.viewAttr}
            wrapperCol={{ span: fieldcol || `${fields.fieldcol}` }}
          >
            <WeaSwitch ecId={`${this && this.props && this.props.ecId || ''}_WeaSwitch@b6cd0t`} fieldConfig={fields} form={form} formParams={formParams} />
          </WeaFormItem>),
          colSpan: 1,
        });
      });
      if (needTigger) {
        group.push(<WeaSearchGroup ecId={`${this && this.props && this.props.ecId || ''}_WeaSearchGroup@zanio3`} col={col} needTigger={needTigger} title={c.title} showGroup={c.defaultshow} items={items} />);
      } else {
        group.push(<WeaSearchGroup ecId={`${this && this.props && this.props.ecId || ''}_WeaSearchGroup@50b79d`} col={col} needTigger={needTigger} showGroup={c.defaultshow} items={items} />);
      }
    });
    return group;
  }

  getColumns = (columns) => {
    let cols = columns ? columns.map((col) => {
      if (col.dataIndex == 'id') {
        col.render = (text, record) => (
            <a href="javascript:;" className="child-item" onClick={() => this.refs.WeaLicenseSubmitCluster.onShow({ addr: record.id })}>
              {record.id}
            </a>);
      }
      if (col.dataIndex == 'addr') {
        col.render = (text, record) => (
            <a href="javascript:;" className="child-item" onClick={() => this.refs.WeaLicenseSubmitCluster.onShow({ addr: record.id })}>
              {record.addr}
            </a>);
      }
      if (col.dataIndex == 'onCount') {
        col.render = (text, record) => (record.isCurrentNode == 'true' ? (
              <div className="wea-field-readonly">
                <a style={{ color: 'blue' }} href="javascript:;" className="child-item" onClick={() => this.refs.WeaNonStandard.onShow()}>
                  {record.onCount}
                </a>
              </div>
            ) : (<div>{record.onCount}</div>));
      }
      return col;
    }) : [];
    return cols;
  }

  onOperatesClick = (record, index, operate) => {
    if (operate.index == '0') {
      this.refs.WeaLicenseSubmitCluster.onShow({ addr: record.id });
    }
  }
}
export default WeaLicenseCluster;
